const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: {
    // 如果有JS文件需要打包，可以在这里配置
    main: './src/js/main.js',
    // page2: './src/js/page2.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'js/[name].[contenthash].js',
    clean: true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'images/[name].[hash][ext]'
        }
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'fonts/[name].[hash][ext]'
        }
      }
    ]
  },
  plugins: [
    // 配置首页
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'index.html',
      chunks: ['main'], // 关联的JS文件
      title: '首页'
    }),
    // 如果有其他页面，继续添加HtmlWebpackPlugin配置
    // new HtmlWebpackPlugin({
    //   template: './src/page2.html',
    //   filename: 'page2.html',
    //   chunks: ['page2'],
    //   title: '第二页'
    // }),
    // 复制其他静态资源（如不需要可删除）
    // new CopyWebpackPlugin({
    //   patterns: [
    //     { from: 'src/assets', to: 'assets' }
    //   ]
    // })
  ],
  devServer: {
    static: {
      directory: path.join(__dirname, 'dist'),
    },
    port: 3000,
    open: true, // 自动打开浏览器
    hot: true, // 热更新
    historyApiFallback: true // 支持HTML5历史模式
  }
};
